﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;
using Runda.CMS.Bll;

namespace Runda.CMS.Portal.NoticeManage
{
    public partial class NoticeSendList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindNoticeSendList(1);
            }
        }
        private void BindNoticeSendList(int pageindex)
        {
            int RecordCount = 0;
            List<CtrtMyNoticeEntity> list = new CtrtMyNoticeBll().GetAll<CtrtMyNoticeEntity>(" and CreatedBy ='" + base.CurrentUserID.ToString() + "' and NoticeType="+System.Convert.ToInt32(Enumerator.NoticeMessageType.Private), "CreatedOn desc", AspNetPager1.PageSize, pageindex, out RecordCount);

            RepeaterNoticeSends.DataSource = list;
            RepeaterNoticeSends.DataBind();
            AspNetPager1.RecordCount = RecordCount;
        }

        protected void RepeaterNoticeSends_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "refersh")
            {
                BindNoticeSendList(1);
            }
        }
        
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindNoticeSendList(AspNetPager1.CurrentPageIndex);
        }

        protected void RepeaterNoticeSends_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            Label noticeID = e.Item.FindControl("noticeID") as Label;
            Label receivers = e.Item.FindControl("receivers") as Label;
            if (noticeID != null) {
                List<SysUserEntity> users = new SysUserBll().GetAll<SysUserEntity>(" and UserID in ( select UserID from Cms_Ctrt_MyNoticeUser where NoticeID = '" + noticeID.Text + "')");
                if (users != null && users.Count > 0) {
                    string receiverStr = "";
                    for (int index = 0; index < users.Count; index++) {
                        if (receiverStr != "") {
                            receiverStr += ";";
                        }
                        receiverStr += users[index].UserName;
                    }
                    receivers.Text = receiverStr;
                }
            }
        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            BindNoticeSendList(AspNetPager1.CurrentPageIndex);
        } 
    }
}
